//
//  MCHomePageCell.swift
//  StoreManage
//
//  Created by MC on 2018/9/3.
//  Copyright © 2018年 MC. All rights reserved.
//

import UIKit

typealias MCHomePageCellClosure = (UIViewController) -> ()


class MCHomePageCell: UITableViewCell {

    var closure : MCHomePageCellClosure?

    public var indexPath : IndexPath = IndexPath()
    
    public class func customCell(tableView:UITableView) -> MCHomePageCell {
        
        let reuseIdentifier = "MCHomePageCell";
        var cell = tableView.dequeueReusableCell(withIdentifier: reuseIdentifier)
        
        cell = MCHomePageCell(style: .default, reuseIdentifier: reuseIdentifier)

        
        return cell as! MCHomePageCell
    }

    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        
        backgroundColor = UIColor.white
        self.addSubview(lineOneView)
        self.addSubview(lineTwoView)
        self.addSubview(lineThreeView)

        self.addSubview(leftButton)
        self.addSubview(rightButton)
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        lineOneView.snp.remakeConstraints { (make) ->Void in
            make.left.right.top.equalTo(0)
            make.height.equalTo(1)
        }

        lineTwoView.snp.remakeConstraints { (make) ->Void in
            make.left.right.bottom.equalTo(0)
            make.height.equalTo(1)
        }
         lineTwoView.snp.remakeConstraints { (make) ->Void in
            make.top.centerX.bottom.equalTo(self)
            make.width.equalTo(1)
        }

        
        
        let width = (MCScreenWidth - 1) / 2
        
        leftButton.snp.remakeConstraints { (make) ->Void in
            make.top.equalTo(1)
            make.bottom.equalTo(-1)
            make.left.equalTo(0)
            make.width.equalTo(width)
        }
        
        rightButton.snp.remakeConstraints { (make) ->Void in
            make.top.equalTo(1)
            make.bottom.equalTo(-1)
            make.right.equalTo(0)
            make.width.equalTo(width)
        }
    }
    
    @objc func itemClicked(sender:UIButton) {
        
        let buttonTag = sender.tag - 1000

        
        if sender.currentImage == nil {
            return
        }
        
        
        switch indexPath {
        case IndexPath.init(row: 0, section: 0):
            if buttonTag == 0 {
//                print("订单管理")
                let vc = MCOrderPageViewController()
                closure?(vc)
            } else {
//                print("订单统计")
                let vc = MCMallStatisticsViewController()
                closure?(vc)
            }
        case IndexPath.init(row: 0, section: 1):
            if buttonTag == 0 {
//                print("店铺商品")
                let vc = MCgoodsListShowViewController()
                closure?(vc)
            } else {
                // 只有店员和店长才可以编辑商品
                if MCUserinfo_getLevel() > MCRole_salesStaff {
                    let vc = MCgoodsListEditViewController()
                    closure?(vc)
                }
            }
        case IndexPath.init(row: 1, section: 1):
            if buttonTag == 0 {
                let vc = MCGoodsCategoryViewController()
                closure?(vc)
            } 
        case IndexPath.init(row: 0, section: 2):
            if buttonTag == 0 {
//                print("店铺信息")
                let vc = MCStoreDetailViewController()
                closure?(vc)
            } else {
//                print("我的账单")
                let vc = MCBillingInfoViewController()
                closure?(vc)
            }
        case IndexPath.init(row: 1, section: 2):
            if buttonTag == 0 {
//                print("店长后台")
                let vc = MCManagerBackgroundViewController()
                closure?(vc)
            } else {
//                print("店员管理")
                let vc = MCClerkManageViewController()
                closure?(vc)
            }
        default:
            break
        }
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    lazy var lineOneView: UIView = {
        let view = UIView()
        view.backgroundColor = UIColor.MCGray_light
        return view
    }()
    
    lazy var lineTwoView: UIView = {
        let view = UIView()
        view.backgroundColor = UIColor.MCGray_light
        return view
    }()

    lazy var lineThreeView: UIView = {
        let view = UIView()
        view.backgroundColor = UIColor.MCGray_light
        return view
    }()

    
    lazy var leftButton: UIButton = {
        let button = UIButton.init(type: UIButtonType.custom)
        button.tag = 1000
        
        button.adjustsImageWhenHighlighted = false

        button.addTarget(self, action: #selector(itemClicked(sender:)), for: .touchUpInside)
        return button
    }()
    
    lazy var rightButton: UIButton = {
        let button = UIButton.init(type: UIButtonType.custom)
        
        button.adjustsImageWhenHighlighted = false
        
        button.tag = 1001
        button.addTarget(self, action: #selector(itemClicked(sender:)), for: .touchUpInside)
        return button
    }()
}
